var httpClient = require('../../utils/httpClient.js');
var utils = require('../../utils/util.js');
Page({
  data: {
    motto: 'Hello World',
    shopInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    scanLogArray: [{ message: "123" }],
    wrongMessage: null,
    connectCode: '',
    settlement: {
      sales: 0,
      salesFormat: "0.00",
      num: 0
    },
    shopId: ""
  },
  onLoad: function (option) {
    var shopId = "";
    if (option == null || option.shopId == null) {
      shopId = wx.getStorageSync("shopId");
    } else {
      shopId = option.shopId;
      wx.setStorageSync("shopId", shopId);
    }
    this.setData({
      shopId: shopId
    })
    this.initGoodsSnap(shopId);
    this.onPullDownRefresh();
  },
  initPage: function (finish) {
    this.getShopInfo(this.data.shopId, finish);
  },
  getSettlemnt: function (shopId, finish) {
    var page = this;
    httpClient.getNoMask("/settlement/getToday/" + shopId, function (result) {
      var settlement = {
        sales: result.sales,
        salesFormat: utils.outputmoney(result.sales),
        num: result.orderNum
      }
      page.setData({
        settlement: settlement
      });
      finish();
    });
  },
  getShopInfo: function (shopId, finish) {
    var page = this;
    httpClient.getNoMask("/shop/get/" + shopId, function (result) {
      page.setData({
        shopInfo: result
      });
      page.getSettlemnt(shopId, finish);
    });
  },
  gotoAddGood: function () {
    wx.navigateTo({
      url: '/pages/addGood/addGood'
    });
  },
  gotoScanGun: function () {
    wx.navigateTo({
      url: '/pages/scanGun/scanGun'
    });
  },
  gotoQueryGood: function () {
    wx.scanCode({
      success: (res) => {
        wx.navigateTo({
          url: '/pages/viewGood/viewGood?code=' + res.result
        });
      }
    })
  },
  gotoCasher: function () {
    wx.navigateTo({
      url: '/pages/casherDesk/casherDesk'
    });
  },
  gotoGoodManager: function () {
    wx.navigateTo({
      url: '/pages/goodManager/goodManager'
    });
  },
  initGoodsSnap: function (shopId) {
    httpClient.getNoMask("/good/getSnap/" + shopId, function (result) {
      wx.setStorageSync("goodsSnap", result);
    });
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.initPage(function () {
      wx.stopPullDownRefresh();
    });
  },
  gotoOrderList: function () {
    wx.navigateTo({
      url: '/pages/orderList/orderList'
    });
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    this.onPullDownRefresh();
  }
})